package com.matt.docpro.controller;

import cn.hutool.core.date.DateUtil;
import com.matt.docpro.config.Utils;
import com.matt.docpro.model.Blog;
import com.matt.docpro.model.LoginInfo;
import com.matt.docpro.model.User;
import com.matt.docpro.service.BlogService;
import com.matt.docpro.service.LoginInfoService;
import com.matt.docpro.service.UserService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * HomeController
 * Created by on 2024/02/05.
 */
@Controller
public class HomeController {

    @Autowired
    UserService userService;
    @Autowired
    LoginInfoService loginInfoService;
    @Autowired
    BlogService blogService;

    @RequestMapping("")
    public String init(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        if (user == null) {
            return "login";
        }
        model.addAttribute("user", user);
        List<Blog> blogs = blogService.getAll();
        model.addAttribute("blogs", blogs);
        return "index";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        loginInfoService.delete();
        return "login";
    }

    @RequestMapping("/register")
    public String register() {
        return "register-page";
    }

    @RequestMapping("/error1")
    public String error() {
        return "error";
    }

    @RequestMapping("/error403")
    public String error403() {
        return "error403";
    }

    @PostMapping("/login/user")
    @ResponseBody
    public Map<String, Object> userLogin(HttpServletRequest request, HttpServletResponse response, String account, String password) {
        Map<String, Object> res = new HashMap<>();
        User user = userService.getUser(account, password);
        if (user != null) {
            String token = UUID.randomUUID().toString();
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setUserId(user.getId());
            loginInfo.setLoginTime(DateUtil.date());
            loginInfo.setToken(token);
            loginInfoService.insert(loginInfo);
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(24*3600);
            cookie.setPath("/");
            response.addCookie(cookie);
            res.put("state", "success");
        } else {
            res.put("state", "fail");
        }
        return res;
    }

    @PostMapping("/login/updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(HttpServletRequest request, String password) {
        Map<String, Object> res = new HashMap<>();
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        userService.updatePassword(user.getAccount(), password);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/register/user")
    @ResponseBody
    public Map<String, Object> userRegister(HttpServletRequest request, User user) {
        Map<String, Object> res = new HashMap<>();
        user.setId("U"+System.currentTimeMillis());
        user.setBaned(false);
        user.setRole("2");
        userService.insert(user);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/about")
    public String about(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        return "about";
    }

    @RequestMapping("/faq")
    public String faq(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        return "faq";
    }

    @RequestMapping("/pricing")
    public String pricing(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        return "pricing";
    }

    @RequestMapping("/submit")
    public String submit(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        return "submit";
    }

    @RequestMapping("/contact")
    public String contact(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        return "contact";
    }

}
